package utils
{
	import flash.utils.flash_proxy;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	import mx.logging.targets.TraceTarget;
	import mx.utils.NameUtil;
	
	/**
	 * 日志工具类 
	 * @author kant
	 * @date 2011-04-19
	 */	
	public class LogUtil
	{
		private static var _instance:LogUtil;
		private var _logger : ILogger ;
		
		public function LogUtil()
		{
			/* Create a target. */
			var logTarget:TraceTarget = new TraceTarget();
			
			/* Log only messages for the classes in the mx.rpc.* and 
			mx.messaging packages. */
			logTarget.filters=["*"];
			
			/* Log all log levels. */
			logTarget.level = LogEventLevel.ALL;
			
			/* Add date, time, category, and log level to the output. */
			logTarget.includeDate = true;
			logTarget.includeTime = true;
			logTarget.includeCategory = true;
			logTarget.includeLevel = true;
			
			/* Begin logging. */
			Log.addTarget(logTarget);
		}
		
		public static function getInst():LogUtil
		{
			if ( _instance == null )
			{
				_instance = new LogUtil();
			}
			return _instance;
		}
		
		public function getLogger( target:Object ):ILogger
		{
			var clazzName:String = null ;
			if ( target is String )
			{
				clazzName = String( target );
			}
			else
			{
				clazzName = NameUtil.getUnqualifiedClassName( target );
				var index:int = clazzName.indexOf("::");
				clazzName = ( index > 0 ) ? clazzName.substring( index , (clazzName.length-index) )
											: clazzName;  
			}
			_logger =  Log.getLogger( clazzName ); 
			return _logger;
		}
		
		public function debug( msg:String ):void
		{
			getInst().debug( msg );
		}
		
		public function warning( msg:String ):void
		{
			_logger.warn( msg );
		}
		
		public function error( msg:String ):void
		{
			_logger.error( msg );
		}
		
		public function info( msg:String ):void
		{
			_logger.info( msg );	
		}	
	}
}